﻿namespace PBIW.Core
{
    [Flags]
    public enum CoreStartingStates
    {
        FullySucceeded = CoreStartingStateLevels.FullySucceeded,
        CannotReadOrWriteSettings = CoreStartingStateLevels.SevereError | CoreStartingProblemsAndErrors.CannotReadOrWriteSettings,
        AutoDisabledZhouyiReferencing = CoreStartingStateLevels.SlightProblem | CoreStartingProblemsAndErrors.AutoDisabledZhouyiReferencing,
    }

    [Flags]
    public enum CoreStartingStateLevels
    {
        FullySucceeded = 0,
        /// <summary>
        /// 包含此值的状态，表示其中有轻微到完全可以忽视的错误。
        /// 收到此状态，可以认为没有错误并正常运行。
        /// </summary>
        SoSlightProblem = 0b0001_0000_0000_0000_0000,
        /// <summary>
        /// 包含此值的状态，表示其中有轻微的、已经由程序采取一定措施自动补救的错误。
        /// 收到此状态，应当告知用户所采取的自动补救措施，并可以正常使用其他大部分功能。
        /// </summary>
        SlightProblem = 0b0010_0000_0000_0000_0000,
        /// <summary>
        /// 包含此值的状态，表示其中有严重导致部分功能异常的错误。
        /// 收到此状态，应当发出错误提示并尽可能正常结束程序。
        /// </summary>
        SevereError = 0b0100_0000_0000_0000_0000,
        /// <summary>
        /// 包含此值的状态，表示其中有严重错误。
        /// 收到此状态，应当给出错误提示并立即结束程序。
        /// </summary>
        SoSevereError = 0b1000_0000_0000_0000_0000,
    }

    [Flags]
    public enum CoreStartingProblemsAndErrors
    {
        CannotReadOrWriteSettings = 0b0000_0000_0000_0001,
        AutoDisabledZhouyiReferencing = 0b0000_0000_0000_0010,
    }

    public static class CoreStartingStatesExtensions
    {
        public static CoreStartingStateLevels TheMostSevere(this CoreStartingStateLevels levels)
        {
            if (levels.HasFlag(CoreStartingStateLevels.SoSevereError))
                return CoreStartingStateLevels.SoSevereError;
            if (levels.HasFlag(CoreStartingStateLevels.SevereError))
                return CoreStartingStateLevels.SevereError;
            if (levels.HasFlag(CoreStartingStateLevels.SlightProblem))
                return CoreStartingStateLevels.SlightProblem;
            if (levels.HasFlag(CoreStartingStateLevels.SoSlightProblem))
                return CoreStartingStateLevels.SoSlightProblem;
            return CoreStartingStateLevels.FullySucceeded;
        }
        public static bool IsFullySucceeded(this CoreStartingStates states)
        {
            return states is CoreStartingStates.FullySucceeded;
        }
        public static CoreStartingStateLevels ToLevels(this CoreStartingStates states)
        {
            return (CoreStartingStateLevels)((int)states & 0b1111_0000_0000_0000_0000);
        }
        public static CoreStartingProblemsAndErrors ToProblemsAndErrors(this CoreStartingStates states)
        {
            return (CoreStartingProblemsAndErrors)((int)states & 0b0000_1111_1111_1111_1111);
        }
    }
}
